<?php
namespace app\navs\event;

use think\Controller;

class Admin extends Controller
{
	protected $query = [];
    
	public function _initialize()
    {
        //$this->request->filter('trim,strip_tags,htmlspecialchars');
        
        $this->query = $this->request->param();
        
		parent::_initialize();
	}
    
    //添加表单
    public function create()
    {
        $this->assign('query', $this->query);
        
        $this->assign('items', model('navs/Fields','loglic')->items($this->query));

		return $this->fetch('navs@admin/create');
    }
    
    //添加数据保存至数据库
	public function save()
    {
        config('common.validate_name', 'navs/Common');
        
        $op_id = model('navs/Db','loglic')->save(input('post.'));
		if($op_id < 1){
			$this->error(model('navs/Db','loglic')->getError());
		}
        
		$this->success(lang('success'));
	}
    
    //删除(数据库)
	public function delete()
    {
		$ids = input('id/a');
		if(!$ids){
			$this->error(lang('mustIn'));
		}
        
        foreach($ids as $id){
            model('navs/Db','loglic')->delete_id($id);
        }
        
        $this->success(lang('success'));
	}
    
    //修改（表单）
	public function edit()
    {
		$id = input('id/d',0);
		if(!$id){
			$this->error(lang('mustIn'));
		}
        
		//查询数据
        $data = model('navs/Db','loglic')->get_id($id, false);
        if( is_null($data) ){
            $this->error(lang('empty'));
        }
        
		$this->assign('query', $this->query);
        
		$this->assign('data', $data);
        
        $this->assign('items', model('navs/Fields','loglic')->items($data));
        
		return $this->fetch('navs@admin/edit');
	}
    
    //修改（数据库）
	public function update()
    {
		$data = input('post.');
        if(!$data['op_id']){
			$this->error(lang('mustIn'));
		}
        
        config('common.validate_name', 'navs/Common');
        
        $info = model('navs/Db','loglic')->update_id($data['op_id'], $data);
        if(is_null($info)){
            $this->error(model('navs/Db','loglic')->getError());
        }
        
        $this->success(lang('success'));
	}

	public function index()
    {
        if($this->request->isAjax()){
            $args = array();
            $args['cache']         = false;
            $args['field']         = '*';
            $args['tree']          = true;
            $args['level']         = true;
            //$args['limit']         = input('pageSize/d', 20);
            //$args['page']          = input('pageNumber/d', 1);
            $args['sort']          = input('get.sortName/s','op_order');
            $args['order']         = input('get.sortOrder/s','asc');
            $args['search']        = input('searchText/s','');
            $args['where']         = DcWhereQuery(['op_module','op_controll','op_action','op_status'], 'eq', $this->query);
            //数据返回
            if($infos = DcNavSelect($args) ){
                return json($infos);
            }
            return json([]);
        }
		//地址栏参数
        $this->assign('query', $this->query);
        //表单筛选字段
        $this->assign('items', model('navs/Fields','loglic')->items($this->query));
        //表格列字段
        $this->assign('columns', model('navs/Fields','loglic')->columns($this->query));
        //加载模板
		return $this->fetch('navs@admin/index');
	}
	
	//排序（拖拽ajax）
	public function sort()
    {
		if( !\daicuo\Op::sort(input('get.id')) ){
            $this->error(lang('fail'));
        }
        DcCacheTag('common/Nav/Item', 'clear');
        $this->success(lang('success'));
	}
    
    //预览
	public function preview()
    {
		$id = input('id/d',0);
		if(!$id){
			$this->error(lang('mustIn'));
		}
		//查询数据
        $data = model('navs/Db','loglic')->get_id($id, false);
        if(!$data['nav_link']){
            $this->error(lang('empty'));
        }
        $this->redirect($data['nav_link'],302);
	}
	
}